package com.bsh.controller.goods;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bsh.dto.CloudUserGoodsListQryDto;
import com.bsh.mybatis.model.CloudUserGoods;
import com.bsh.service.UserGoodsService;
import com.bsh.utils.PageTools;
import com.bsh.utils.Pager;
import com.mysql.jdbc.StringUtils;

@Controller
public class CloudUserGoodsController {
	private static Logger log = LoggerFactory.getLogger(CloudUserGoodsController.class);

	@Autowired
	HttpServletRequest request;
	@Autowired
	HttpServletResponse response;
	@Autowired
	UserGoodsService userGoodsServ;


	/**
	 * 用户兑换列表
	 */
	@RequestMapping(value = "/goods/cloudUserGoodsList")
	public ModelAndView merchantList(@ModelAttribute CloudUserGoodsListQryDto dto) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/goods/cloudUserGoodsList");

		String pageStr = request.getParameter("pageIndex");
		Integer pageIndex = Integer.parseInt(StringUtils.isNullOrEmpty(pageStr) ? "1" : pageStr);

		String sizeStr = request.getParameter("pageSize");
		Integer pageSize = Integer.parseInt(StringUtils.isNullOrEmpty(sizeStr) ? "10" : sizeStr);

		Pager pager = new Pager(pageIndex, pageSize);
		List<Map<String, Object>> list = userGoodsServ.getPage(pager, dto);
		mv.addObject("list", list);
		mv.addObject("pager", pager);
		mv.addObject("qry", dto);
		return mv;
	}
	
	
	/**
	 * 添加发货消息
	 */
	@ResponseBody
	@RequestMapping(value = "/goods/audit/pass/{gid}")
	public ModelAndView passMsg(@PathVariable(value = "gid") Integer gid) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/goods/sendGoodsAudit");
		mv.addObject("gid", gid);
		return mv;
	}

	/**
	 * 发货
	 */
	@ResponseBody
	@RequestMapping(value = "/goods/audit/pass")
	public ModelAndView pass(@RequestParam(value = "gid") Integer gid, @RequestParam(value = "msg") String auditMsg) {
		Integer res = userGoodsServ.sendGoods(gid, true, auditMsg);
		ModelAndView mv = passMsg(gid);
		mv.addObject("gid", gid);
		mv.addObject("msg", auditMsg);

		String msg = "";
		if (res == 1)
			msg = PageTools.alertCloseFraRefresh("发货", "发货成功！", 1000);
		else
			msg = PageTools.alert("发货", "发货失败！", 1000);
		mv.addObject("actionMsg", msg);
		return mv;
	}
	
	
	/**
	 * 添加取消发货消息
	 */
	@ResponseBody
	@RequestMapping(value = "/goods/audit/cancel/{gid}")
	public ModelAndView cancelMsg(@PathVariable(value = "gid") Integer gid) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/goods/cancelGoodsAudit");
		mv.addObject("gid", gid);
		return mv;
	}	
	
	/**
	 * 取消发货
	 */
	@ResponseBody
	@RequestMapping(value = "/goods/audit/cancel")
	public ModelAndView cancel(@RequestParam(value = "gid") Integer gid, @RequestParam(value = "msg") String auditMsg) {
		Integer res = userGoodsServ.sendGoods(gid, false, auditMsg);
		ModelAndView mv = cancelMsg(gid);
		mv.addObject("gid", gid);
		mv.addObject("msg", auditMsg);

		String msg = "";
		if (res == 1)
			msg = PageTools.alertCloseFraRefresh("取消发货", "取消成功！", 1000);
		else
			msg = PageTools.alert("发货", "发货失败！", 1000);
		mv.addObject("actionMsg", msg);
		return mv;
	}
	
	/**
	 * 查看发货备注
	 */
	@ResponseBody
	@RequestMapping(value = "/goods/remark/{gid}")
	public ModelAndView viewRemark(@PathVariable(value = "gid") Integer gid) {
		CloudUserGoods goods = userGoodsServ.getUserGoods(gid);
		ModelAndView mv = new ModelAndView();		
		mv.setViewName("/pages/goods/viewRemark");
		mv.addObject("msg", goods.getRemark());
		return mv;
	}

}
